package com.cloud.controller;

import com.cloud.Service.UserInfoService;
import com.cloud.config.Log;
import com.cloud.myInterface.Factory;
import com.cloud.pojo.UserInfo;
import com.cloud.tools.CommonResult;
import com.cloud.tools.CommonResultFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author: Tangtao
 * @Date: 2022/4/11 13:05
 * @Description: User personal information management control layer
 * @ClassName: UserInfoController
 */
@RestController
public class UserInfoController {

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private PasswordEncoder passwordEncoder;

    private final Factory resultFactory = new CommonResultFactory();


    /* 用户注册 */
    @Log(value = "成为大家庭一员")
    @PostMapping(value = "/user/register")
    public CommonResult register(@RequestBody UserInfo userInfo){
        userInfo.setPassword(passwordEncoder.encode(userInfo.getPassword()));
        if(this.userInfoService.register(userInfo))
            return resultFactory.getResultInstance(200,"操作成功",1);
        return resultFactory.getResultInstance(500,"操作失败，请重试",null);
    }


    /* 用户手机号登录 */
    @PostMapping(value = "/user/loginByPhone")
    public CommonResult loginByPhone(@RequestBody Map<String,String> map){
        System.out.println(map);
        System.out.println("到达 /user/loginByPhone");
        Map<String,String> mapRes = userInfoService.loginByPhoneNumber(map);
        return resultFactory.getResultInstance(200,"登录成功",mapRes);

    }

    /* 用户身份证登录 */
    @PostMapping(value = "/user/loginByCardId")
    public CommonResult loginByCardId(@RequestBody Map<String,String> map){
        Map<String,String> mapRes = userInfoService.loginByCardId(map);
        System.out.println("到达 /user/loginByCardId");
        return resultFactory.getResultInstance(200,"登录成功",mapRes);
    }



    /* 用户修改个人信息 */
    @PostMapping(value = "/user/updateInfo")
//    @PreAuthorize("hasAnyAuthority('userInfo:update')")
    public CommonResult updateInfo(@RequestBody Map<String,String> map){
        if(userInfoService.updateUserInfo(map))
            return resultFactory.getResultInstance(200,"操作成功",1);
        return resultFactory.getResultInstance(500,"操作失败，请重试",null);
    }

    @GetMapping(value = "/user/getInfo")
//    @PreAuthorize("hasAnyAuthority('userInfo:read')")
    public CommonResult getInfo(){
        Map<String,String> info = null;
        info = userInfoService.getInfo();
        if(info != null)
            return resultFactory.getResultInstance(200,"操作成功",info);
        return resultFactory.getResultInstance(500,"操作失败，请重试",null);
    }

    @Log(value = "更改密码")
    @PostMapping(value = "/user/updatePwd")
//    @PreAuthorize("hasAnyAuthority('userInfo:update')")
    public CommonResult updatePwd(@RequestBody Map<String,String> map){
        map.put("newPwd",passwordEncoder.encode(map.get("newPwd")));
        if(userInfoService.updatePwd(map))
            return resultFactory.getResultInstance(200,"操作成功",1);
        return resultFactory.getResultInstance(500,"操作失败，请重试",null);
    }

    @GetMapping(value = "/user/getAllUserInfo")
//    @PreAuthorize("hasAnyAuthority('userInfo:read')")
    public CommonResult getAllUserInfo(){
        List<UserInfo> allUserInfo = userInfoService.getAllUserInfo();
        return resultFactory.getResultInstance(200,"操作成功",allUserInfo);
    }

    @Log(value = "删除用户")
    @PostMapping(value = "/user/delete")
//    @PreAuthorize("hasAnyAuthority('userInfo:delete')")
    public CommonResult deleteUserInfo(@RequestBody Map<String,String> map){
        if(userInfoService.deleteUserInfo(map))
            return resultFactory.getResultInstance(200,"操作成功",1);
        return resultFactory.getResultInstance(500,"操作失败，请重试",null);
    }

    @Log(value = "修改用户类型")
    @PostMapping(value = "/user/updateType")
//    @PreAuthorize("hasAnyAuthority('userInfo:update')")
    public CommonResult updateType(@RequestBody Map<String,String> map){
        if(userInfoService.updateUserType(map))
            return resultFactory.getResultInstance(200,"操作成功",1);
        return resultFactory.getResultInstance(500,"操作失败，请重试",null);
    }

}
